package com.ruoyi.project.dict.dictspider.controller;

import com.ruoyi.common.utils.JsonUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.dict.dictspider.domain.ZdicSpider;
import com.ruoyi.project.dict.dictspider.service.IZdicSpiderService;
import com.ruoyi.project.dict.zi.domain.Zi;
import com.ruoyi.project.dict.zi.service.IZiService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 汉典内容Controller
 *
 * @author shixiangwen
 * @date 2021-03-05
 */
@Controller
@RequestMapping("/dict/dictspider")
public class ZdicSpiderController extends BaseController {

    private String prefix = "dict/dictspider";

    @Autowired
    private IZdicSpiderService zdicSpiderService;

    @Autowired
    private IZiService ziService;


    @RequiresPermissions("dict:dictspider:view")
    @GetMapping()
    public String dictspider() {
        return prefix + "/dictspider";
    }

    /**
     * 查询汉典内容列表
     */
    @RequiresPermissions("dict:dictspider:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZdicSpider zdicSpider) {
        startPage();
        List<ZdicSpider> list = zdicSpiderService.selectZdicSpiderList(zdicSpider);
        return getDataTable(list);
    }

    /**
     * 导出汉典内容列表
     */
    @RequiresPermissions("dict:dictspider:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZdicSpider zdicSpider) {
        List<ZdicSpider> list = zdicSpiderService.selectZdicSpiderList(zdicSpider);
        ExcelUtil<ZdicSpider> util = new ExcelUtil<ZdicSpider>(ZdicSpider.class);
        return util.exportExcel(list, "dictspider");
    }

    /**
     * 新增汉典内容
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存汉典内容
     */
    @RequiresPermissions("dict:dictspider:add")
    @Log(title = "汉典内容", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZdicSpider zdicSpider) {
        return toAjax(zdicSpiderService.insertZdicSpider(zdicSpider));
    }

    /**
     * 修改汉典内容
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ZdicSpider zdicSpider = zdicSpiderService.selectZdicSpiderById(id);
        mmap.put("zdicSpider", zdicSpider);
        return prefix + "/edit";
    }

    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, ModelMap mmap) {
        ZdicSpider zi = zdicSpiderService.selectZdicSpiderById(id);
        mmap.put("zi", zi);


        if (StringUtils.isNotEmpty(zi.getGycdFmt())) {
            mmap.put("gycdJson", JsonUtil.toListObject(zi.getGycdFmt(), Map.class));
        }

        return prefix + "/view";
    }

    @GetMapping("/swjz/{id}")
    public String swjz(@PathVariable("id") Long id, ModelMap mmap) {
        ZdicSpider zi = zdicSpiderService.selectZdicSpiderById(id);
        mmap.put("zi", zi);
        return prefix + "/swjz";
    }

    @GetMapping("/kxzd/{id}")
    public String kxzd(@PathVariable("id") Long id, ModelMap mmap) {
        ZdicSpider zi = zdicSpiderService.selectZdicSpiderById(id);
        mmap.put("zi", zi);
        return prefix + "/kxzd";
    }

    @GetMapping("/gycdJsonEdit/{id}")
    public String gycdJsonEdit(@PathVariable("id") Long id, ModelMap mmap) {
        ZdicSpider zi = zdicSpiderService.selectZdicSpiderById(id);
        Zi zi2 = ziService.selectZiById(20013L);
        //zi.setGycdFmt(zi2.getGuhanyuJson());
        mmap.put("zi", zi);
        return prefix + "/gycdJsonEdit";
        //return "/dict/zi/guhanyuJsonEdit";
    }


    /**
     * 修改保存汉典内容
     */
    @RequiresPermissions("dict:dictspider:edit")
    @Log(title = "汉典内容", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZdicSpider zdicSpider) {
        return toAjax(zdicSpiderService.updateZdicSpider(zdicSpider));
    }

    /**
     * 删除汉典内容
     */
    @RequiresPermissions("dict:dictspider:remove")
    @Log(title = "汉典内容", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(zdicSpiderService.deleteZdicSpiderByIds(ids));
    }
}
